﻿using CozeDomain.IServices;
using CozeDto.AdminDtos;
using CozeDto.LoginDtos;
using CozeDto.UserDtos;
using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace CozeWapApi
{
    /// <summary>
    /// 登录模块
    /// </summary>
    [Route("/api/[controller]/")]
    public class LoginController : BaseApiController
    {
        private readonly IUserService _userService;

        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="userService"></param>
        public LoginController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="inputLogin"></param>
        /// <returns></returns>
        [HttpPost("login"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [AllowAnonymous]
        public async Task<ApiResult<string>> Login([FromBody] ReqLogin inputLogin)
        {
            return await _userService.LoginAsync(inputLogin.Account, inputLogin.Password);
        }

        /// <summary>
        /// 请求刷新Token（以旧换新）
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet("refreshToken"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<string>> RefreshToken(string token = "")
        {
            return await _userService.RefreshTokenAsync(token);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary> 
        /// <returns></returns>
        [HttpGet(template: "getUserInfo"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepUserInfo>> GetUserInfo()
        {
            if (LoginId == null)
            {
                return new ApiResult<RepUserInfo>()
                {
                    Code = StatusCodeEnum.Waring,
                    Msg = "登录过期，请重新登录"
                };
            }
            return await _userService.GetUserAsync(LoginId);
        }

        /// <summary>
        /// 登出操作
        /// </summary>
        /// <returns></returns>

        [HttpPost("logout"), TypeFilter(typeof(ExtActionFilterAttribute))]
        public ApiResult<int> Logout()
        {
            //await HttpContext.SignOutAsync(JwtBearerDefaults.AuthenticationScheme);
            return new ApiResult<int>();
        }
    }
}